package com.djdg.foodies.common.base;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.collections.CollectionUtils;

import java.io.Serializable;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description: 分页对象
 * @Auther Demon
 * @Date 2018/3/13 15:10 星期二
 */
@ApiModel("分页信息")
public class PageResult<E> implements Serializable {


    private static final long serialVersionUID = 1L;

    @ApiModelProperty("总页数")
    private int totalPage;
    @ApiModelProperty("页码")
    private int pageNum;
    @ApiModelProperty("总记录数")
    private long totalRecord;
    @ApiModelProperty("每页数量")
    private int pageSize;
    @ApiModelProperty("是否有下一页")
    private boolean hasNext;
    @ApiModelProperty("请求数据")
    private List<E> data;

    public PageResult() {
    }

    public PageResult(com.github.pagehelper.Page<E> page) {
        if(page != null) {
            this.pageSize = page.getPageSize();
            this.pageNum = page.getPageNum();
            this.data = page.getResult();
            this.hasNext = page.getPageNum() < page.getPages();
            this.totalRecord = page.getTotal();
            this.totalPage = page.getPages();
        }
    }

    public PageResult(List<E> data) {
        if(CollectionUtils.isNotEmpty(data) && data instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page page = (com.github.pagehelper.Page) data;
            this.pageSize = page.getPageSize();
            this.pageNum = page.getPageNum();
            this.data = (List<E>) page.getResult();
            this.hasNext = page.getPageNum() < page.getPages();
            this.totalRecord = page.getTotal();
            this.totalPage = page.getPages();
        }
    }

    /**
     * 需要转换VO时
     * @param data 原始类型数据
     * @param function 转换方法
     * @param <R>
     */
    public <R> PageResult(List<R> data, Function<R, E> function) {
        if(CollectionUtils.isNotEmpty(data) && data instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page page = (com.github.pagehelper.Page) data;
            this.pageSize = page.getPageSize();
            this.pageNum = page.getPageNum();
            this.data = ((com.github.pagehelper.Page<R>) data).getResult().stream().map(function).collect(Collectors.toList());
            this.hasNext = page.getPageNum() < page.getPages();
            this.totalRecord = page.getTotal();
            this.totalPage = page.getPages();
        }
    }

    public <R> PageResult(org.springframework.data.domain.Page<R> page, Function<R, E> function) {
        if (page != null) {
            this.pageSize = page.getSize();
            this.pageNum = page.getNumber();
            this.data = page.getContent().stream().map(function).collect(Collectors.toList());
            this.hasNext = page.getNumber() < page.getTotalPages();
            this.totalRecord = page.getTotalElements();
            this.totalPage = page.getTotalPages();
        }

    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public long getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(long totalRecord) {
        this.totalRecord = totalRecord;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public boolean isHasNext() {
        return hasNext;
    }

    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }

    public List<E> getData() {
        return data;
    }

    public void setData(List<E> data) {
        this.data = data;
    }
}
